<?php
/**
 * Created by PhpStorm.
 * User: user1
 * Date: 2018/9/3
 * Time: 11:04
 */

namespace app\admin\controller;


use app\common\service\LoginService;
use app\common\validate\LoginValidate;
use think\Controller;
use think\Exception;
use think\facade\Session;

class Login extends Controller
{
    // 登录页面
    public function index() {

        // 如果已登录，直接进入首页
        if (session('?szb_admin')) {
            $this->redirect(url('/admin/index/index'));
        }

        return view();
    }

    /**
     * 验证登录信息
     * @return \think\response\Json
     * @throws Exception
     */
    public function check_login() {
        (new LoginValidate())->goCheck(); // 验证前台提交的数据

//        $login_validate = new LoginValidate();
//        $login_validate->goCheck();
//        $param = $login_validate->getDataByRule(input('param.'));

        try {
            LoginService::checkLogin(); // 验证登录信息
            return return_data('登录成功！');
        } catch (Exception $e) {
            session('szb_admin', null);
            return return_data($e->getMessage(), 1);
        }
    }

    // 退出登录
    public function logout() {
        Session::clear();
        $this->error('退出成功！', url('index'));
    }
}